package com.abysshal.icarte.datamodel;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.abysshal.icarte.datamodel.interf.MessageRecvInterf;
import com.abysshal.icarte.datamodel.interf.MessageSendInterf;
import com.abysshal.icarte.datamodel.net.Packet;
import com.abysshal.icarte.datamodel.net.Server;
import com.abysshal.icarte.datamodel.obj.Category;
import com.abysshal.icarte.datamodel.obj.Menu;
import com.abysshal.icarte.datamodel.obj.Order;
import com.abysshal.icarte.datamodel.persistence.HibernateUtil;
import com.abysshal.icarte.datamodel.util.GSonTools;

public class Core implements MessageRecvInterf{
	private static final Core core = new Core();
	public static Core getCore() { return core;}
	
	private Server server;
	private DataModel dm;
	
	private Core(){
		HibernateUtil.getSessionFactory(); // Just call the static initializer of that class    
		dm = new DataModel();
		testinit(dm);
	}
	
	public void start(){
		server = new Server(Config.SERVER_LISTEN_PORT, this);
	}
	
	public void stop(){
		server.close();
	}

	@Override
	public void recv(String read, MessageSendInterf msi) {
		Packet p = GSonTools.json2bean(read, Packet.class);		
		String action = p.getAction();
		if (Config.VERBOSE < 2) MyLog.logd("[Core]Action recv:" + action);
		Packet res = new Packet();
		res.setTime(new Date());
		try {
			if (action.equals("getversion")){
				res.setAction("version");
				res.setContent("" + Config.MENU_VERSION);
			} else if (action.equals("listmenu")){
				Packet tmp = new Packet();
				tmp.setTime(new Date());
				tmp.setAction("starttransfer");
				tmp.setContent("" + dm.totalMenus());
				msi.send(GSonTools.bean2json(tmp, Packet.class));
				Iterator<Menu> it = dm.getMenuIterator();
				tmp.setAction("menudata");
				while (it.hasNext()){
					tmp.setTime(new Date());
					tmp.setContent(GSonTools.bean2json(it.next(), Menu.class));
					msi.send(GSonTools.bean2json(tmp, Packet.class));
				}
				res.setAction("endtransfer");
				res.setContent("SUCC");//TODO
			} else if (action.equals("listcategory")){
				Packet tmp = new Packet();
				tmp.setTime(new Date());
				tmp.setAction("starttransfer");
				tmp.setContent("" + dm.totalCategorys());
				msi.send(GSonTools.bean2json(tmp, Packet.class));
				Iterator<Category> it = dm.getCategoryIterator();
				tmp.setAction("categorydata");
				while (it.hasNext()){
					tmp.setTime(new Date());
					tmp.setContent(GSonTools.bean2json(it.next(), Category.class));
					msi.send(GSonTools.bean2json(tmp, Packet.class));
				}
				res.setAction("endtransfer");
				res.setContent("SUCC");//TODO
			} else if (action.equals("starttable")){
				int desknum = Integer.parseInt(p.getContent());
				int rt = dm.startTable(desknum);
				res.setContent("" + rt);
				res.setAction("tablestatus");
			} else if (action.equals("submitorder")){
				Order order = GSonTools.json2bean(p.getContent(), Order.class);
				int rt = dm.createOrder(order);
				res.setContent("" + rt);
				res.setAction("orderstatus");
			} else if (action.equals("getorder")){
				Long oid = Long.parseLong(p.getContent());
				Order rt = dm.getOrder(oid);
				res.setContent(GSonTools.bean2json(rt, Order.class));
				res.setAction("orderdata");
			} else if (action.equals("updateorder")){
				Order order = GSonTools.json2bean(p.getContent(), Order.class);
				int rt = dm.updateOrder(order);
				res.setContent("" + rt);
				res.setAction("orderstatus");
			} else {
			}
		} catch (Exception ex){
			p.setAction("error");			
		}
		
		msi.send(GSonTools.bean2json(res, Packet.class));
	}
	
	public static void main(String args[]){
		Core.getCore().start();
	}
	
	private void testinit(DataModel dm){
		Map<Long, Menu> menus = new HashMap();
    	Menu m = new Menu();
    	m.setDescription("Menu 1 desc\n Another line");
    	m.setId(1l);
    	m.setName("Menu 1 Name");
    	m.setOnsale(true);
    	m.setOrigPic("http://localhost/origpic.png");
    	m.setRank(4);
    	m.setStartSaleTime(new Date());
    	m.setThumbPic("https://localhost:8080/thumbpic.png");
    	menus.put(m.getId(), m);
      	Menu m2 = new Menu();
    	m2.setDescription("Menu 2 desc\nAnother line\nline3");
    	m2.setId(2l);
    	m2.setName("Menu 2 Name");
    	m2.setOnsale(true);
    	m2.setOrigPic("http://localhost/origpic2.png");
    	m2.setRank(5);
    	m2.setStartSaleTime(new Date());
    	m2.setThumbPic("https://localhost:8080/thumbpic2.png");
    	menus.put(m2.getId(), m2);  
    	dm.addMenu(m);
    	dm.addMenu(m2);
    	
    	Category c = new Category();
    	c.setId(0l);
    	c.setName("Test Category1");
    	dm.addCategory(c);
	}
}
